package com.hyopt.modules.app.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.hyopt.common.utils.Constant;
import com.hyopt.common.utils.DateUtils;
import com.hyopt.common.utils.FileUtils;
import com.hyopt.common.utils.R;
import com.hyopt.modules.app.form.UserInfoForm;
import com.hyopt.modules.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * TODO 会员相关操作接口
 *
 * @author crazypenguin
 * @version 1.0.0
 * @createdate 2019/1/11
 */
@RestController
@RequestMapping("/app/user")
@Api(value="AppUserController",tags={"APP会员操作相关接口"})
@Slf4j
public class AppUserController {

    public static void main(String[] args) {
        String url = "https://chatgpt.ruolikongjian.cn/chat/say";
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("say","描述樱花的七言律诗");
        String body = HttpUtil.createPost(url)
                .header("Content-Type", "application/json")
                .body(jsonObject.toString()).execute().body();
        System.out.println(body);
    }


    @Autowired
    private UserService userService;

    /**
     * 修改用户信息
     *
     * @param
     * @return
     */
    @PostMapping("/info")
    @ApiOperation("修改用户信息")
    public R UserInfo(@RequestBody UserInfoForm form){
        if(!userService.updateInfo(form)){
            return R.error("未知异常");
        }
        return R.ok();
    }

    /**
     * 上传头像--用户
     *
     * @param request
     * @return
     */
    @ApiOperation("上传图片")
    @PostMapping("upload/img")
    public R  updateImg(HttpServletRequest request) {
        try {
            String path = Constant.USER_PUT + DateUtils.format(new Date(),DateUtils.DATE_PATTERN) + "/";
            String img = FileUtils.springUpload(request,path);
            return R.ok().put("data","/img/user/" + DateUtils.format(new Date(),DateUtils.DATE_PATTERN) + "/" + img);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
}
